<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>英法神经翻译系统</title>
    <link href="https://fonts.googleapis.com/css2?family=Poppins:wght@300;400;500;600&display=swap" rel="stylesheet">
    <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.4.0/css/all.min.css">
    <style>
        * {
            margin: 0;
            padding: 0;
            box-sizing: border-box;
        }

        body {
            font-family: 'Poppins', 'Arial', sans-serif;
            background: linear-gradient(135deg, #667eea 0%, #efd64a 100%);
            min-height: 100vh;
            padding: 20px;
            color: #333;
        }

        .container {
            max-width: 1000px;
            margin: 0 auto;
            background: white;
            border-radius: 15px;
            box-shadow: 0 10px 30px rgba(0, 0, 0, 0.2);
            overflow: hidden;
        }

        .header {
            background: linear-gradient(135deg, #667eea 0%, #efd64a 100%);
            color: white;
            padding: 30px;
            text-align: center;
            position: relative;
        }

        .header h1 {
            font-size: 2.5em;
            margin-bottom: 10px;
            font-weight: 600;
        }

        .header p {
            font-size: 1.2em;
            opacity: 0.9;
            margin-bottom: 20px;
        }

        .header-buttons {
            display: flex;
            justify-content: center;
            gap: 15px;
            margin-top: 15px;
            flex-wrap: wrap;
        }

        .header-button {
            padding: 12px 25px;
            background: rgba(255, 255, 255, 0.2);
            color: white;
            border: 2px solid rgba(255, 255, 255, 0.3);
            border-radius: 8px;
            cursor: pointer;
            font-size: 14px;
            font-weight: bold;
            transition: all 0.3s;
            backdrop-filter: blur(10px);
            text-decoration: none;
            display: inline-flex;
            align-items: center;
            justify-content: center;
            gap: 8px;
        }

        .header-button:hover {
            background: rgba(255, 255, 255, 0.3);
            border-color: rgba(255, 255, 255, 0.5);
            transform: translateY(-2px);
            text-decoration: none;
            color: white;
        }

        .csdn-button {
            background: rgba(255, 255, 255, 0.2);
            border-color: rgba(255, 255, 255, 0.4);
        }

        .csdn-button:hover {
            background: rgba(255, 255, 255, 0.3);
            border-color: rgba(255, 255, 255, 0.6);
        }

        .main-content {
            padding: 30px;
            display: grid;
            grid-template-columns: 1fr 1fr;
            gap: 30px;
        }

        @media (max-width: 768px) {
            .main-content {
                grid-template-columns: 1fr;
            }
        }

        .input-section, .output-section {
            background: #c2eeee;
            border-radius: 12px;
            padding: 25px;
            box-shadow: 0 5px 15px rgba(0, 0, 0, 0.05);
        }

        .section-title {
            display: flex;
            align-items: center;
            gap: 10px;
            margin-bottom: 20px;
            padding-bottom: 15px;
            border-bottom: 2px solid #e9ecef;
        }

        .section-title h3 {
            font-size: 1.4em;
            color: #4facfe;
            font-weight: 500;
        }

        .flag-icon {
            font-size: 1.8rem;
        }

        .text-input-wrapper {
            position: relative;
            margin-bottom: 20px;
        }

        textarea {
            width: 100%;
            height: 180px;
            padding: 16px;
            border: 2px solid #ddd;
            border-radius: 12px;
            resize: vertical;
            font-family: 'Poppins', sans-serif;
            font-size: 16px;
            transition: all 0.3s ease;
            background: white;
        }

        textarea:focus {
            outline: none;
            border-color: #4facfe;
            box-shadow: 0 0 0 3px rgba(79, 172, 254, 0.15);
        }

        .char-count {
            position: absolute;
            bottom: 10px;
            right: 15px;
            font-size: 0.85rem;
            color: #6c757d;
        }

        .translate-button {
            display: flex;
            align-items: center;
            justify-content: center;
            gap: 10px;
            width: 100%;
            background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
            color: white;
            border: none;
            padding: 16px;
            border-radius: 12px;
            cursor: pointer;
            font-size: 1.1rem;
            font-weight: 500;
            transition: all 0.3s ease;
            box-shadow: 0 4px 15px rgba(67, 97, 238, 0.3);
        }

        .translate-button:hover {
            transform: translateY(-2px);
            box-shadow: 0 6px 20px rgba(67, 97, 238, 0.4);
        }

        .translate-button:active {
            transform: translateY(0);
        }

        .translate-button.loading {
            opacity: 0.8;
            cursor: not-allowed;
        }

        .translation-result {
            position: relative;
            min-height: 180px;
            border: 2px solid #e9ecef;
            border-radius: 12px;
            padding: 16px;
            background: white;
        }

        .translation-text {
            min-height: 140px;
            font-size: 16px;
            line-height: 1.6;
        }

        .placeholder {
            color: #6c757d;
            font-style: italic;
        }

        .copy-button {
            position: absolute;
            top: 15px;
            right: 15px;
            background: white;
            border: 1px solid #e9ecef;
            border-radius: 8px;
            padding: 8px;
            cursor: pointer;
            color: #6c757d;
            transition: all 0.2s ease;
        }

        .copy-button:hover {
            background: #4facfe;
            color: white;
        }

        .examples-section {
            padding: 25px;
            background: #f8f9fa;
            border-top: 1px solid #e9ecef;
        }

        .examples-title {
            display: flex;
            align-items: center;
            gap: 10px;
            margin-bottom: 20px;
            font-weight: 500;
            color: #333;
        }

        .examples-list {
            display: grid;
            grid-template-columns: repeat(auto-fit, minmax(250px, 1fr));
            gap: 15px;
        }

        .example-item {
            display: flex;
            flex-direction: column;
            padding: 15px;
            background: white;
            border-radius: 12px;
            border-left: 4px solid #4facfe;
            box-shadow: 0 3px 10px rgba(0, 0, 0, 0.05);
            transition: transform 0.3s;
        }

        .example-item:hover {
            transform: translateY(-3px);
        }

        .example-text {
            flex: 1;
            font-size: 0.95rem;
            margin-bottom: 10px;
        }

        .example-use-btn {
            background: #4facfe;
            color: white;
            border: none;
            padding: 8px 15px;
            border-radius: 8px;
            cursor: pointer;
            font-size: 0.9rem;
            transition: all 0.2s ease;
            align-self: flex-end;
        }

        .example-use-btn:hover {
            background: #667eea;
        }

        .loading-spinner {
            display: flex;
            align-items: center;
            gap: 10px;
            color: #4facfe;
        }

        .error-message {
            display: flex;
            align-items: center;
            gap: 10px;
            color: #e74c3c;
        }

        .notification {
            position: fixed;
            bottom: -100px;
            left: 50%;
            transform: translateX(-50%);
            padding: 15px 25px;
            border-radius: 10px;
            color: white;
            font-weight: 500;
            box-shadow: 0 5px 15px rgba(0, 0, 0, 0.15);
            transition: bottom 0.3s ease;
            z-index: 1000;
        }

        .notification.show {
            bottom: 30px;
        }

        .notification.success {
            background: #4facfe;
        }

        .notification.error {
            background: #e74c3c;
        }

        footer {
            text-align: center;
            padding: 20px;
            color: white;
            background: linear-gradient(135deg, #667eea 0%, #efd64a 100%);
        }

        @media (max-width: 768px) {
            .header h1 {
                font-size: 2em;
            }

            .header-buttons {
                flex-direction: column;
                align-items: center;
            }

            .header-button {
                width: 200px;
            }
        }
    </style>
</head>
<body>
    <div class="container">
        <div class="header">
            <h1>英法神经翻译系统</h1>
            <p>基于Seq2Seq与注意力机制的GRU翻译模型</p>
            <div class="header-buttons">
                <button class="header-button" onclick="openNotes()">
                    <i class="fas fa-book"></i> 项目简介
                </button>
                <a href="https://blog.csdn.net/your_username/your_article" class="header-button csdn-button" target="_blank" rel="noopener noreferrer">
                    <i class="fab fa-codepen"></i> 项目CSDN链接
                </a>
            </div>
        </div>

        <div class="main-content">
            <div class="input-section">
                <div class="section-title">
                    <span class="flag-icon">🇬🇧</span>
                    <h3>英文输入</h3>
                </div>
                <div class="text-input-wrapper">
                    <textarea id="english-text" placeholder="请输入英文句子..." maxlength="200"></textarea>
                    <div class="char-count"><span id="char-count">0</span>/200</div>
                </div>
                <button id="translate-btn" class="translate-button">
                    <i class="fas fa-exchange-alt"></i>
                    <span>翻译</span>
                </button>
            </div>

            <div class="output-section">
                <div class="section-title">
                    <span class="flag-icon">🇫🇷</span>
                    <h3>法语翻译</h3>
                </div>
                <div class="translation-result">
                    <div id="french-translation" class="translation-text">
                        <div class="placeholder">翻译结果将显示在这里...</div>
                    </div>
                    <button id="copy-btn" class="copy-button" title="复制到剪贴板">
                        <i class="far fa-copy"></i>
                    </button>
                </div>
            </div>
        </div>

        <div class="examples-section">
            <div class="examples-title">
                <i class="fas fa-lightbulb"></i>
                <h3>示例句子</h3>
            </div>
            <div class="examples-list">
                <div class="example-item">
                    <div class="example-text">i m impressed with your french .</div>
                    <button class="example-use-btn" data-text="i m impressed with your french .">使用此例句</button>
                </div>
                <div class="example-item">
                    <div class="example-text">i m more than a friend .</div>
                    <button class="example-use-btn" data-text="i m more than a friend .">使用此例句</button>
                </div>
                <div class="example-item">
                    <div class="example-text">she is beautiful like her mother .</div>
                    <button class="example-use-btn" data-text="she is beautiful like her mother .">使用此例句</button>
                </div>
            </div>
        </div>

        <footer>
            <p>Powered by Seq2Seq with Attention • Neural Machine Translation</p>
        </footer>
    </div>

    <div class="notification" id="notification">
        <span id="notification-text"></span>
    </div>

    <script>
        document.addEventListener('DOMContentLoaded', function() {
            const englishText = document.getElementById('english-text');
            const translateBtn = document.getElementById('translate-btn');
            const frenchTranslation = document.getElementById('french-translation');
            const copyBtn = document.getElementById('copy-btn');
            const charCount = document.getElementById('char-count');
            const notification = document.getElementById('notification');
            const exampleUseBtns = document.querySelectorAll('.example-use-btn');

            // 字符计数
            englishText.addEventListener('input', function() {
                const count = englishText.value.length;
                charCount.textContent = count;

                if (count > 200) {
                    charCount.style.color = '#e74c3c';
                } else {
                    charCount.style.color = '#6c757d';
                }
            });

            // 示例使用按钮
            exampleUseBtns.forEach(btn => {
                btn.addEventListener('click', function() {
                    englishText.value = this.getAttribute('data-text');
                    charCount.textContent = englishText.value.length;
                });
            });

            // 翻译按钮
            translateBtn.addEventListener('click', function() {
                const text = englishText.value.trim();

                if (!text) {
                    showNotification('请输入英文句子', 'error');
                    return;
                }

                if (text.length > 200) {
                    showNotification('文本过长，请限制在200字符以内', 'error');
                    return;
                }

                // 添加加载状态
                translateBtn.classList.add('loading');
                translateBtn.innerHTML = '<i class="fas fa-spinner fa-spin"></i> 翻译中...';
                frenchTranslation.innerHTML = '<div class="loading-spinner"><i class="fas fa-spinner fa-spin"></i> 翻译中...</div>';

                // 发送翻译请求到后端API
                fetch('/translate', {
                    method: 'POST',
                    headers: {
                        'Content-Type': 'application/json',
                    },
                    body: JSON.stringify({ text: text })
                })
                .then(response => {
                    if (!response.ok) {
                        throw new Error('网络响应错误');
                    }
                    return response.json();
                })
                .then(data => {
                    if (data.error) {
                        frenchTranslation.innerHTML = `<div class="error-message"><i class="fas fa-exclamation-circle"></i> ${data.error}</div>`;
                        showNotification('翻译失败: ' + data.error, 'error');
                    } else {
                        frenchTranslation.innerHTML = data.translation;
                        showNotification('翻译完成', 'success');
                    }
                })
                .catch(error => {
                    frenchTranslation.innerHTML = `<div class="error-message"><i class="fas fa-exclamation-circle"></i> 网络错误: ${error.message}</div>`;
                    showNotification('网络错误: ' + error.message, 'error');
                })
                .finally(() => {
                    translateBtn.classList.remove('loading');
                    translateBtn.innerHTML = '<i class="fas fa-exchange-alt"></i> 翻译';
                });
            });

            // 复制按钮
            copyBtn.addEventListener('click', function() {
                const textToCopy = frenchTranslation.innerText;

                if (!textToCopy || textToCopy === '翻译结果将显示在这里...') {
                    showNotification('没有内容可复制', 'error');
                    return;
                }

                navigator.clipboard.writeText(textToCopy)
                    .then(() => {
                        showNotification('已复制到剪贴板', 'success');
                    })
                    .catch(err => {
                        showNotification('复制失败', 'error');
                    });
            });

            // 显示通知
            function showNotification(message, type) {
                const notificationText = document.getElementById('notification-text');
                notificationText.textContent = message;
                notification.className = 'notification show ' + type;

                setTimeout(() => {
                    notification.classList.remove('show');
                }, 3000);
            }

            // 按Enter键翻译（Ctrl+Enter换行）
            englishText.addEventListener('keydown', function(e) {
                if (e.key === 'Enter' && !e.ctrlKey) {
                    e.preventDefault();
                    translateBtn.click();
                }
            });
        });

        function openNotes() {
    window.open('/notes', '_blank');
}
    </script>
</body>
</html>